# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# @ECLASS: lua.eclass
# @MAINTAINER: rafael@rafaelmartins.com
# @BLURB: A utility eclass that should be inherited by anything that deals with
# Lua modules.
# @DESCRIPTION: Some useful functions for dealing with Lua. Based on Python eclass

inherit multilib

if [[ -n "${NEED_LUA}" ]]; then
	LUA_ATOM=">=dev-lang/lua-${NEED_LUA}"
else
	LUA_ATOM="dev-lang/lua"
fi

DEPEND="${LUA_ATOM}"
RDEPEND="${DEPEND}"

__lua_version_extract() {
	local verstr=$1
	export LUAVER_MAJOR=${verstr:0:1}
	export LUAVER_MINOR=${verstr:2:1}
	if [[ ${verstr:3:1} == . ]]; then
		export LUAVER_MICRO=${verstr:4}
	fi
	export LUAVER="${LUAVER_MAJOR}.${LUAVER_MINOR}"
}

__lua_version() {
	[[ -n "${LUAVER}" ]] && return 0
	local tmparr
	lua=${lua:-/usr/bin/lua}
	tmparr=( $(${lua} -v 2>&1 ) )
	export LUAVER_ALL="${tmparr[1]}"
	__lua_version_extract $LUAVER_ALL
}

__lua_install_paths() {
	export LUA_LMOD="/usr/share/lua/${LUAVER}"
	export LUA_CMOD="/usr/$(get_libdir)/lua/${LUAVER}"
}


lua_cmodules_install() {
	local module=$(basename ${1})
	einfo "Installing shared object module: ${2}/${module}"
	install -Dm0755 "${1}" "${D}/${LUA_CMOD}/${2:-.}/${module}"
}

lua_lmodules_install() {
	local module=$(basename ${1})
	einfo "Installing Lua Module: ${2}/${module}"
	install -Dm0644 "${1}" "${D}/${LUA_LMOD}/${2:-.}/${module}"
}

__lua_version
__lua_install_paths
